﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace CSP.Package.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public partial class InstallationSelector : UserControl
    {
        public InstallationSelector()
        {
            InitializeComponent();
        }

        #region Members
        protected Installer _form;
        public Installer GetForm()
        {
            return _form;
        }

        public void SetForm(Installer installer)
        {
            _form = installer;
        }
        #endregion

        #region Events

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InstallationSelector_Load(object sender, EventArgs e)
        {
            TranslateLiterals();

            int setupModality = 0;
            int installationStep = 0;

            // an existing installation exists or has been finished
            if (RegistryAccess.PropertyExists(Registry.LocalMachine, Constants.CspRegistryKey, "SetupModality"))
            {
                setupModality = Convert.ToInt16(RegistryAccess.GetStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "SetupModality"));

                // the previous installation ended
                if (!RegistryAccess.PropertyExists(Registry.LocalMachine, Constants.CspRegistryKey, "InstallationProcessStep"))
                {

                    // the previous installation was with SimpleCMS, so we allow upgrade
                    if (setupModality == 1)
                    {
                        rbUpgrade.Checked = true;
                        rbUpgrade.Enabled = true;
                        rbMossAndCrm.Enabled = false;
                        rbMossOnly.Enabled = false;
                    }
                    else
                    {
                        // otherwise, upgrade is not possible
                        MessageBox.Show(CSP.Package.labels.CspInstallError_Upgrade, CSP.Package.labels.CspInstallError_Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Application.Exit();
                    }
                }
                else
                {
                    // The previous installation didn't end
                    installationStep = Convert.ToInt16(RegistryAccess.GetStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "InstallationProcessStep"));

                    if (installationStep!=0)
                        GetForm().BeginInstallation(setupModality);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
      /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            int installation = 0;


            if (rbMossOnly.Checked)
                installation = 1;
            if (rbMossAndCrm.Checked)
                installation = 2;
            if (rbUpgrade.Checked)
                installation = 3;

            if (installation >= 2)
            {
              if (!RegistryAccess.PropertyExists(Registry.LocalMachine, @"SOFTWARE\Microsoft\MSCRM", "ServerUrl"))
              {
                MessageBox.Show(CSP.Package.labels.CspInstallError_CRMNotInstalled, CSP.Package.labels.CspInstallError_Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
              }
            }

            CSP.Package.RegistryAccess.CreateStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "SetupModality", installation.ToString());
            GetForm().BeginInstallation(installation);
        }

        #endregion

      

        private void TranslateLiterals()
        {
            label1.Text = CSP.Package.labels.InstallationSelector_Intro;

            this.rbMossAndCrm.Text = CSP.Package.labels.InstallSelectorMossAndCrm_Label;
            this.rbMossOnly.Text = CSP.Package.labels.InstallSelectorMossOnly_Label;
            this.rbUpgrade.Text = CSP.Package.labels.InstallSelectorUpgrade_Label;

            this.btnStart.Text = CSP.Package.labels.InstallSelectorStart_Label; 
        }


    }
}
